#include "dbi_testzjd.h"
#define SQL_TESTZJD_INSERT "INSERT INTO test.testzjd SET name='%s', gender=%d, address='%s';"
#define SQL_TESTZJD_SELECTONE "SELECT * FROM test.testzjd WHERE id=%d;"
#define SQL_TESTZJD_SELECT "SELECT * FROM test.testzjd WHERE %s LIMIT %d,%d;"
#define SQL_TESTZJD_COUNT "SELECT COUNT(*) AS _COUNT_ FROM test.testzjd WHERE %s;"
#define SQL_TESTZJD_UPDATEONE "UPDATE test.testzjd SET name='%s', gender=%d, address='%s' WHERE id=%d;"
#define SQL_TESTZJD_UPDATE "UPDATE test.testzjd SET name='%s', gender=%d, address='%s' WHERE %s;"
#define SQL_TESTZJD_DELETEONE "DELETE FROM test.testzjd WHERE id=%d;"
#define SQL_TESTZJD_DELETE "DELETE FROM test.testzjd WHERE %s;"

int32_t DBITestzjd::Insert(Testzjd &in_Testzjd)
{
	AYOO::MySQLWrap mys= *( this->GetMySQL() );
	try
	{
		mys.Connect(this->m_sHost, this->m_iPort, this->m_sHost, this->m_sPswd);
		in_Testzjd.Escape();
		mys.QueryWith(SQL_TESTZJD_INSERT
			, in_Testzjd.name.c_str()
			, in_Testzjd.gender
			, in_Testzjd.address.c_str()
		);
	}
	catch(AYOO::AyooException &ex)
	{
	//	LOG_DEB(ex.what());
	}
	mys.Close();
	
	return 0;
}

int32_t DBITestzjd::SelectOne(uint32_t id, Testzjd &out_Testzjd)
{
	AYOO::MySQLWrap mys= *( this->GetMySQL() );
	int32_t iRes = 0;
	try
	{
		mys.Connect(this->m_sHost, this->m_iPort, this->m_sHost, this->m_sPswd);
		mys.QueryWith(SQL_TESTZJD_SELECTONE
			, id
		);
		mys.StoreResult();
		if(mys.FetchRow())
		{
			out_Testzjd.id = mys.GetField_Int("id");
			out_Testzjd.name = mys.GetField_String("name");
			out_Testzjd.gender = mys.GetField_Int("gender");
			out_Testzjd.address = mys.GetField_String("address");
			iRes = 1;
		}
		mys.Close();
	}
	catch(AYOO::AyooException &ex)
	{
	//	LOG_DEB(ex.what());
	}
	mys.Close();
	return iRes;
}

int32_t DBITestzjd::Select(const std::string &sCondition, uint32_t iStart, uint32_t iLimit, std::vector<Testzjd> &outlist_Testzjd)
{
	AYOO::MySQLWrap mys= *( this->GetMySQL() );
	try
	{
		mys.Connect(this->m_sHost, this->m_iPort, this->m_sHost, this->m_sPswd);
		mys.QueryWith(SQL_TESTZJD_SELECT
			, sCondition.c_str()
			, iStart
			, iLimit
		);
		mys.StoreResult();
		while(mys.FetchRow())
		{
			Testzjd st;
			st.id = mys.GetField_Int("id");
			st.name = mys.GetField_String("name");
			st.gender = mys.GetField_Int("gender");
			st.address = mys.GetField_String("address");
			outlist_Testzjd.push_back(st);
		}
		mys.Close();
	}
	catch(AYOO::AyooException &ex)
	{
	//	LOG_DEB(ex.what());
	}
	mys.Close();
	return outlist_Testzjd.size();
}

int32_t DBITestzjd::Count(const std::string &sCondition)
{
	AYOO::MySQLWrap mys= *( this->GetMySQL() );
	int32_t iCount = 0;
	try
	{
		mys.Connect(this->m_sHost, this->m_iPort, this->m_sHost, this->m_sPswd);
		mys.QueryWith(SQL_TESTZJD_SELECTONE
			, sCondition.c_str()
		);
		mys.StoreResult();
		if(mys.FetchRow())
		{
			iCount = mys.GetField_Int("_COUNT_");
		}
		mys.Close();
	}
	catch(AYOO::AyooException &ex)
	{
	//	LOG_DEB(ex.what());
	}
	mys.Close();
	return iCount;
}

int32_t DBITestzjd::UpdateOne(Testzjd &in_Testzjd)
{
	AYOO::MySQLWrap mys= *( this->GetMySQL() );
	try
	{
		mys.Connect(this->m_sHost, this->m_iPort, this->m_sHost, this->m_sPswd);
		in_Testzjd.Escape();
		mys.QueryWith(SQL_TESTZJD_UPDATEONE 
			, in_Testzjd.name.c_str()
			, in_Testzjd.gender
			, in_Testzjd.address.c_str()
			, in_Testzjd.id
		);
	}
	catch(AYOO::AyooException &ex)
	{
	//	LOG_DEB(ex.what());
	}
	mys.Close();
	
	return 0;
}

int32_t DBITestzjd::Update(const std::string &sCondition, Testzjd &in_Testzjd)
{
	AYOO::MySQLWrap mys= *( this->GetMySQL() );
	try
	{
		mys.Connect(this->m_sHost, this->m_iPort, this->m_sHost, this->m_sPswd);
		in_Testzjd.Escape();
		mys.QueryWith(SQL_TESTZJD_UPDATE
			, in_Testzjd.name.c_str()
			, in_Testzjd.gender
			, in_Testzjd.address.c_str()
			, sCondition.c_str()
		);
	}
	catch(AYOO::AyooException &ex)
	{
	//	LOG_DEB(ex.what());
	}
	mys.Close();
	return 0;
}

int32_t DBITestzjd::DeleteOne(uint32_t id)
{
	AYOO::MySQLWrap mys= *( this->GetMySQL() );
	try
	{
		mys.Connect(this->m_sHost, this->m_iPort, this->m_sHost, this->m_sPswd);
		mys.QueryWith(SQL_TESTZJD_DELETEONE
			, id
		);
	}
	catch(AYOO::AyooException &ex)
	{
	//	LOG_DEB(ex.what());
	}
	mys.Close();
	return 0;
}

int32_t DBITestzjd::Delete(const std::string &sCondition)
{
	AYOO::MySQLWrap mys= *( this->GetMySQL() );
	try
	{
		mys.Connect(this->m_sHost, this->m_iPort, this->m_sHost, this->m_sPswd);
		mys.QueryWith(SQL_TESTZJD_DELETE
			, sCondition.c_str()
		);
	}
	catch(AYOO::AyooException &ex)
	{
	//	LOG_DEB(ex.what());
	}
	mys.Close();
	return 0;
}

int32_t main(int32_t argc, char* argv[])
{
	return 0;
}
